package com.yolib.sanyi.pic;


import com.yolib.sanyi.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.widget.ImageView;


public class PicImageView extends ImageView {
	LoadCallback mLoadCallback = new LoadCallback(this);
	private Context  mContext;
	public static final int SIZE_SMALL = 0;
	public static final int SIZE_LARGE = 1;

	public PicImageView(Context context) {
		this(context, null);
	}

	public PicImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public PicImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}

	public void setNullPic() {
		mLoadCallback.setId("null");
	}

	public void setPic(String fileID)
	{
		mLoadCallback.isEdit = false;
		mLoadCallback.setId(fileID);
		ImageLoader.getPicBitmap(mContext, fileID, mLoadCallback);
	}

	public void setEditPic(String fileID)
	{
		mLoadCallback.isEdit = true;
		mLoadCallback.setId(fileID);
		ImageLoader.getPicBitmap(mContext, fileID, mLoadCallback);
	}

	public void setImageView(Bitmap bitmap, int size) {
		//		Bitmap mask;
		//		switch(size)
		//		{
		//		case SIZE_SMALL:
		//						mask = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.bg_list_avatar);
		//						break;
		//		case SIZE_LARGE:
		//		default:
		//			mask = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher);
		//			break;
		//		}
		//		if(bitmap.getWidth() < mask.getWidth() || bitmap.getHeight() < mask.getHeight())
		//		{
		//			float scale = (float)((float)mask.getWidth()/(float)bitmap.getWidth()) > (float)((float)mask.getHeight()/(float)bitmap.getHeight())?
		//					(float)((float)mask.getWidth()/(float)bitmap.getWidth()) : (float)((float)mask.getHeight()/(float)bitmap.getHeight());
		//					Matrix matrix = new Matrix(); 
		//					matrix.postScale(scale, scale);
		//					bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		//		}
		//		Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
		//
		//		Canvas c = new Canvas(result);
		//		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		//		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		//		c.drawBitmap(bitmap, mask.getWidth()/2-bitmap.getWidth()/2, mask.getHeight()/2-bitmap.getHeight()/2, null);
		//		c.drawBitmap(mask, 0, 0, paint);
		//		paint.setXfermode(null);
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		// 計算縮放比例
		float scaleWidth = ((float) 200) / width;
		float scaleHeight = ((float) 200) / height;
		// 取得想要縮放的matrix參數
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的圖片
		Bitmap newbm = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		this.setImageBitmap(newbm);
	}

	public void setEditIamgeView(Bitmap bitmap) {
		//		Bitmap mask;
		//		mask = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher);
		//		if(bitmap.getWidth() < mask.getWidth() || bitmap.getHeight() < mask.getHeight())
		//		{
		//			float scale = (float)((float)mask.getWidth()/(float)bitmap.getWidth()) > (float)((float)mask.getHeight()/(float)bitmap.getHeight())?
		//					(float)((float)mask.getWidth()/(float)bitmap.getWidth()) : (float)((float)mask.getHeight()/(float)bitmap.getHeight());
		//					Matrix matrix = new Matrix(); 
		//					matrix.postScale(scale, scale);
		//					bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		//		}
		//		Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
		//
		//		Canvas c = new Canvas(result);
		//		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		//		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		//		c.drawBitmap(bitmap, mask.getWidth()/2-bitmap.getWidth()/2, mask.getHeight()/2-bitmap.getHeight()/2, null);
		//		c.drawBitmap(mask, 0, 0, paint);
		//
		//		mask = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.bg_avatar_cover);
		//		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
		//		c.drawBitmap(mask, 0, 0, paint);
		//
		//		paint.setXfermode(null);
		//		this.setImageBitmap(result);
	}


}
